//
//  BuyCarTableViewCell.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/1.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "BuyCarTableViewCell.h"

@interface BuyCarTableViewCell()

@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UILabel *buyLabel;

@end

@implementation BuyCarTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.layer.masksToBounds = YES;
        _imgView.backgroundColor = getColor(@"f1f2f6");
        
        _infoLabel = [UILabel new];
        _infoLabel.numberOfLines = 0;
        _infoLabel.font = DEF_FontSize_13;
        
        _selectBtn = [UIButton new];
        [_selectBtn setImage:[UIImage imageNamed:@"choose_nor"] forState:UIControlStateNormal];
        [_selectBtn setImage:[UIImage imageNamed:@"choose_sel"] forState:UIControlStateSelected];
        [_selectBtn addTarget:self action:@selector(selectEvent:) forControlEvents:UIControlEventTouchUpInside];
        _selectBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        _buyLabel = [UILabel new];
        _buyLabel.numberOfLines = 2;
        _buyLabel.text = @"立即\n购买";
        _buyLabel.textColor = [UIColor whiteColor];
        _buyLabel.backgroundColor = getColor(mainColor);
        _buyLabel.layer.masksToBounds = YES;
        _buyLabel.layer.cornerRadius = 8;
        _buyLabel.font = [UIFont systemFontOfSize:12 weight:1];
        _buyLabel.textAlignment = NSTextAlignmentCenter;
        _buyLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(buyLabelCkick)];
        [_buyLabel addGestureRecognizer:tap];
        
        [self.contentView addSubview:_imgView];
        [self.contentView addSubview:_infoLabel];
        [self.contentView addSubview:_selectBtn];
        [self.contentView addSubview:self.buyLabel];
    }

    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    _selectBtn.frame = CGRectMake(0, 0, 40, 40);
    _selectBtn.center = CGPointMake(_selectBtn.center.x, self.frame.size.height/2.);
    _imgView.frame = CGRectMake(CGRectGetMaxX(_selectBtn.frame)+5, 0, self.frame.size.height-15, self.frame.size.height-15);
    _imgView.center = CGPointMake(_imgView.center.x, self.frame.size.height/2.);

    _infoLabel.frame = CGRectMake(CGRectGetMaxX(_imgView.frame)+10, 0, SCREEN_WIDTH - CGRectGetMaxX(_imgView.frame) - 60, self.frame.size.height);
    _buyLabel.frame = CGRectMake(SCREEN_WIDTH-50, 0, 40, 40);
    _buyLabel.center = CGPointMake(_buyLabel.center.x, self.frame.size.height/2.);
}

- (void)selectEvent:(UIButton *)sender{
    if (!sender.selected) {
        if (_getAddChooseClick) {
            _getAddChooseClick();
        }
    }else{
        if (_getDeleteChooseClick) {
            _getDeleteChooseClick();
        }
    }
}

- (void)setDataDictionary:(NSDictionary *)dataDictionary{
    _dataDictionary = dataDictionary;
//    if (self.chooseArray.count == 0) {
//        self.selectBtn.selected = NO;
//    }
    if ([self.chooseArray containsObject:dataDictionary[@"sc_id"]]) {
        self.selectBtn.selected = YES;
    }else{
        self.selectBtn.selected = NO;
    }
   
    NSString *title = dataDictionary[@"c_name"];
    NSString *info = dataDictionary[@"c_introduce"];
    NSString *money = [NSString stringWithFormat:@"¥%@",dataDictionary[@"unit_price"]];
    NSString *temp = [NSString stringWithFormat:@"%@\n%@\n%@",title,info,money];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:temp];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"545454") range:[temp rangeOfString:info]];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(redColor) range:[temp rangeOfString:money]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:4];//调整行间距
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [temp length])];
    
    _infoLabel.attributedText = attStr;
    [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:dataDictionary[@"first_picture"]]];
}

- (void)buyLabelCkick{
    if (_getBuyNowClick) {
        _getBuyNowClick();
    }
}

@end
